package com.example.mbl.service.impl;

import com.example.mbl.dao.PostDoctorMapper;
import com.example.mbl.dao.TeacherMapper;
import com.example.mbl.dto.TeacherDto;
import com.example.mbl.entity.PostDoctor;
import com.example.mbl.entity.Teacher;
import com.example.mbl.interceptor.AppToken;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.PostDoctorPageResult;
import com.example.mbl.result.business.TeacherPageResult;
import com.example.mbl.result.business.TeacherResult;
import com.example.mbl.service.TeacherService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private PostDoctorMapper postDoctorMapper;

    @Override
    public WebRestResult addTeacher(Teacher teacher) {
        WebRestResult result = new WebRestResult();
        teacher.setTeacherid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(teacher.getFlag()))
            teacher.setFlag("0");
        teacher.setCreatedate(new Date());
        if(teacherMapper.insert(teacher)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增教师失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteTeacher(Teacher teacher) {
        WebRestResult result = new WebRestResult();
        if(teacherMapper.deleteByPrimaryKey(teacher.getTeacherid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除教师失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateTeacher(Teacher teacher) {
        WebRestResult result = new WebRestResult();
        if(teacherMapper.updateByPrimaryKeySelective(teacher)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改教师失败");
        }
        return result;
    }

    @Override
    public WebRestResult setTeacherVisible(Teacher teacher) {
        WebRestResult result = new WebRestResult();
        if(teacherMapper.setTeacherVisible(teacher.getTeacherid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<TeacherPageResult> getTeacherList(int page, int pagesize) {
        PageResult<TeacherPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<TeacherPageResult> list = teacherMapper.getTeacherList(start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public TeacherResult getTeacherItem(String teacherid) {
        TeacherResult result = teacherMapper.getTeacherItem(teacherid);
        if(!Tools.isEmpty(result)){
            result.setArticles(teacherMapper.getTeacherArticles(teacherid));
            result.setEducations(teacherMapper.getTeacherEducations(teacherid));
            result.setAwards(teacherMapper.getTeacherAwards(teacherid));
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取详情失败");
        }
        return result;
    }

    @Override
    public WebRestResult addTeacherEducation(TeacherDto teacherDto) {
        WebRestResult result = new WebRestResult();
        if(teacherDto.getEducationList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        if(teacherMapper.addTeacherEducation(teacherDto)==teacherDto.getEducationList().size()){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增教育经历失败");
        }
        return result;
    }

    @Override
    public WebRestResult addTeacherAward(TeacherDto teacherDto) {
        WebRestResult result = new WebRestResult();
        if(teacherDto.getAwardList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        if(teacherMapper.addTeacherAward(teacherDto)==teacherDto.getAwardList().size()){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增任职经历失败");
        }
        return result;
    }

    @Override
    public WebRestResult addTeacherArticle(TeacherDto teacherDto){
        WebRestResult result = new WebRestResult();
        if(teacherDto.getArticleList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        if(teacherMapper.addTeacherArticle(teacherDto)==teacherDto.getArticleList().size()){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增教师文章失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteTeacherEducation(TeacherDto teacherDto) {
        WebRestResult result = new WebRestResult();
        if(teacherMapper.deleteTeacherEducation(teacherDto.getId())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteTeacherAward(TeacherDto teacherDto) {
        WebRestResult result = new WebRestResult();
        if(teacherMapper.deleteTeacherAward(teacherDto.getId())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteTeacherArticle(TeacherDto teacherDto){
        WebRestResult result = new WebRestResult();
        if(teacherMapper.deleteTeacherArticle(teacherDto.getId())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败");
        }
        return result;
    }

    @Override
    public WebRestResult addPostDoctor(PostDoctor postDoctor) {
        WebRestResult result = new WebRestResult();
        postDoctor.setId(UUIDUtil.randomUUID());
        if(Tools.isEmpty(postDoctor))
            postDoctor.setFlag("0");
        postDoctor.setCreatedate(new Date());
        if(postDoctorMapper.insert(postDoctor)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增博士后失败");
        }
        return result;
    }

    @Override
    public WebRestResult deletePostDoctor(PostDoctor postDoctor) {
        WebRestResult result = new WebRestResult();
        if(postDoctorMapper.deleteByPrimaryKey(postDoctor.getId())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除博士后失败");
        }
        return result;
    }

    @Override
    public WebRestResult updatePostDoctor(PostDoctor postDoctor) {
        WebRestResult result = new WebRestResult();
        if(postDoctorMapper.updateByPrimaryKeySelective(postDoctor)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新博士后失败");
        }
        return result;
    }

    @Override
    public PageResult<PostDoctorPageResult> getPostDoctorList(int page, int pagesize) {
        PageResult<PostDoctorPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<PostDoctorPageResult> list = postDoctorMapper.getPostDoctorList(start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
